﻿using System;
using System.Linq;
using System.ComponentModel;
using System.Windows.Data;
using System.Collections.ObjectModel;

using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;

using SystemModule.Models;
using SystemModule.Services;
using ServiceAgent;
using ServiceAgent.SystemService;
using Infrastructure;
using Infrastructure.Helpers;
using Infrastructure.Events;
using System.Windows.Input;
using System.Windows;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using SystemModule.Views;
using Infrastructure.InteractionRequests;

namespace SystemModule.ViewModels
{
    /// <summary>
    /// ViewModel for AddNhomNguoiDungView.
    /// </summary>
    public class AddNhomNguoiDungViewModel : NotificationObject, INavigationAware, IPopupWindowActionAware
    {
        #region Fields
        private readonly IEventAggregator eventAggregator;
        private readonly IUnityContainer container;
        private readonly IRegionManager regionManager;

        ObservableCollection<QuyenDTO> quyenDonViList;

        ObservableCollection<QuyenDTO> quyenList;

        string tenGoi;
        string moTa;
        bool isCapTruong;

        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor.
        /// </summary>
        public AddNhomNguoiDungViewModel(IEventAggregator eventAggregator, IRegionManager regionManager, IUnityContainer container)
        {
            this.eventAggregator = eventAggregator;
            this.regionManager = regionManager;
            this.container = container;
            this.Initialize();

            quyenDonViList = new ObservableCollection<QuyenDTO>();
        }
        #endregion

        #region Administrative Properties
        // IPopupWindowActionAware implementation
        public Window HostWindow { get; set; }

        public Notification HostNotification { get; set; }

        public string TenGoi
        {
            get { return tenGoi; }
            set
            {
                tenGoi = value;
                RaisePropertyChanged(() => this.TenGoi);
            }
        }

        public string MoTa
        {
            get { return moTa; }
            set
            {
                moTa = value;
                RaisePropertyChanged(() => this.MoTa);
            }
        }

        public bool IsCapTruong
        {
            get { return isCapTruong; }
            set
            {
                isCapTruong = value;
                RaisePropertyChanged(() => this.IsCapTruong);
            }
        }

        public ObservableCollection<QuyenDTO> QuyenList
        {
            get
            {
                return quyenList;
            }
            set
            {
                quyenList = value;
                RaisePropertyChanged(() => this.QuyenList);
            }
        }

        public ObservableCollection<QuyenDTO> QuyenDonViList
        {
            get { return quyenDonViList; }
            set
            {
                quyenDonViList = value;
                RaisePropertyChanged(() => this.QuyenDonViList);
            }
        }
        #endregion

        #region Command Properties
        public ICommand AddNewCommand { get; private set; }
        public ICommand CancelCommand { get; set; }
        #endregion

        #region Private Methods
        private void Initialize()
        {
            this.AddNewCommand = new DelegateCommand(this.ExecuteAddNew);
            this.CancelCommand = new DelegateCommand(this.ExecuteCancel);

            this.GetQuyenList();
        }

        void ExecuteAddNew()
        {
            try
            {
                using (var proxy = container.Resolve<SystemServiceAgent>())
                {
                    DonViDTO obj = new DonViDTO();
                    obj.TenGoi = this.TenGoi;
                    obj.MoTa = this.MoTa;
                    obj.IsCapTruong = this.IsCapTruong;
                    proxy.SaveDonViWithQuyenList(obj, this.QuyenDonViList);
                    if (this.HostWindow != null)
                    {
                        this.HostWindow.Close();
                    }
                }
            }
            catch (System.Exception)
            {
                MessageBox.Show("Có lỗi");
            }
        }

        void ExecuteCancel()
        {
            if (this.HostWindow != null)
            {
                this.HostWindow.Close();
            }
        }

        void GetQuyenList()
        {
            try
            {
                using (var proxy = container.Resolve<SystemServiceAgent>())
                {
                    QuyenList = proxy.GetQuyenList();
                }
            }
            catch (Exception)
            {

            }
        }

        #endregion

        #region INavigationAware Members
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            // Called to see if this view can handle the navigation request.
            // If it can, this view is activated.
            return false;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

            // Called when this view is deactivated as a result of navigation to another view.
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            //// Called to initialize this view during navigation.

            //// Retrieve any required paramaters from the navigation Uri.
            //string id = navigationContext.Parameters["ID"];
            //if (!string.IsNullOrEmpty(id))
            //{
            //    DataItem item = _dataService.GetModel().FirstOrDefault(dataItem => dataItem.Id == id);
            //}
        }
        #endregion
    }
}
